<?php
namespace slinks\extensions\twig\core;
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


use slinks\SlinksPaths;

use slinks\common\UniversalClassLoader;

use slinks\core\Templating\EngineInterface;

use slinks\core\Templating\TemplateNameParserInterface;

use slinks\core\DependencyInjection\ContainerInterface;
use slinks\core\Templating\TemplateNameParser;

/**
 * This engine knows how to render Twig templates.
 *
 * @author Fabien Potencier <fabien.potencier@symfony-project.com>
 * @author Navid Mitchell
 * 
 * @Service('twig.engine')
 * @Scope(public=false)
 * @Argument(@Service('twig'))
 */
class TwigEngine implements EngineInterface{
 
    /**
     * @var \Twig_Environment does the actual template handling
     */
    protected $environment;
    
    protected $parser;
    /**
     * @var array $templateLoaders keyed by file suffix that will be matched to given loader and values is loader.
     */
    protected $templateLoaders=array();

    /**
     *
     */
    public function __construct(\Twig_Environment $environment){
        $this->parser = new TemplateNameParser();
        $loader = new UniversalClassLoader();
        $loader->registerPrefix('Twig',SlinksPaths::twigDir());
        $loader->register();
       
        $this->environment = $environment;
    }
    

    /**
     * Renders a template.
     *
     * @param mixed $name       A template name
     * @param array $parameters An array of parameters to pass to the template
     *
     * @return string The evaluated template as a string
     *
     * @throws \InvalidArgumentException if the template does not exist
     * @throws \RuntimeException         if the template cannot be rendered
     */
    public function render($name, array $parameters = array()){
        return $this->load($name)->render($parameters);
    }


    /**
     * Loads the given template.
     *
     * @param mixed $name A template name
     *
     * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
     *
     * @throws \Twig_Error_Loader if the template cannot be found
     */
    public function load($name){
        $template = $this->parser->parse($name);
        $this->environment->setLoader($this->templateLoaders[$template['engine']]);
        
        return $this->environment->loadTemplate($name);
    }

    /**
     * Returns true if this class is able to render the given template.
     *
     * @param string $name A template name
     *
     * @return Boolean True if this class supports the given resource, false otherwise
     */
    public function supports($name){
        $template = $this->parser->parse($name);
        return array_key_exists($template['engine'], $this->templateLoaders);
    }

    public function hasExtension($name){
        return $this->environment->hasExtension($name);
    }

    public function getExtension($name){
        return $this->environment->getExtension($name);
    }

    public function addExtension(\Twig_ExtensionInterface $extension){
        $this->environment->addExtension($extension);
    }

    public function removeExtension($name){
        $this->environment->removeExtension($extension);
    }

    public function addLoader(ITwigTemplateLoader $loader){
        $this->templateLoaders[$loader->getTemplateSuffix()] = $loader;
    }
    

}
